<?php if(navigation::getAdminAction() === 'create' || navigation::getAdminAction() === 'update' ): ?>
<?php
if(navigation::getAdminModeleId()>0){
    $publication = new modele_publication(navigation::getAdminModeleId());
    $data = $publication->getDatas();
    $data['permalien'] = $publication->getPermalien()->getCleanedLien(false);
    if($data['summary']===$publication->generateSummary()) $data['summary'] = '';
    $action = 'update';
    
    // ajout de tag à la création
    if(isset($_GET['action'])&&$_GET['action']==='create'&&isset($_GET['do'])&&$_GET['do']==1&&isset($_GET['tag_id'])&&$_GET['tag_id']>0){
        $tag = new modele_tag($_GET['tag_id']);
        $tag->addPublication($publication->getId());
    }
}else{
    $action = 'create';
    $data = array();
}

?>
<div class="columns thirteen">
    <h1>        
        <a class="pwe-ui-btn" href="?template=publication<?php echo (isset($_GET['category_id']))?'&category_id='.$_GET['category_id']:''; ?><?php echo (isset($_GET['tag_id']))?'&tag_id='.$_GET['tag_id']:''; ?>">
            <span class="ui-icon ui-icon ui-icon-arrowthick-1-w"></span>
        </a> Modifier/créer un article</h1>
    <hr style="margin: 5px 0 15px 0;">
</div>
<form style="margin: 12px 0;" method="post" enctype="multipart/form-data" action="?template=publication&action=<?php echo $action; ?>&modele=publication&do=1&modele_id=<?php echo (isset($data['id']))?$data['id']:0; ?><?php echo (isset($_GET['category_id']))?'&category_id='.$_GET['category_id']:''; ?><?php echo (isset($_GET['tag_id']))?'&tag_id='.$_GET['tag_id']:''; ?>">
    <div class="columns nine">
        <div align="right">
            <input type="submit" value="Enregistrer" class="pwe-ui-btn"/> 
            <?php if(isset($data['id'])&&$data['id']>0): ?><a href="<?php echo $data['url']; ?>" target="_blank" title="voir la publication" class="pwe-ui-btn">voir</a><?php endif; ?>
        </div>
        <?php echo $this->getInputHtml('Titre', 'modele_title', (isset($data['title']))?$data['title']:'', 'publication', (isset($data['id']))?$data['id']:0, array('class_input'=>'titreModele','actions'=>array('onblur'=>"javascript:generatePermalien($(this).val());"))); ?>
        <?php echo $this->getInputHtml('Permalien', 'modele_lien', (isset($data['permalien']))?$data['permalien']:'', 'publication', (isset($data['id']))?$data['id']:0, array('prefix'=>PWE::getBaseURL(),'sufix'=>modele_permalien::getExtensionByModele('publication'),'id_input'=>'permalien','actions'=>array('onblur'=>"javascript:validPermalien($(this).val());"))); ?>
        <?php echo $this->getTextareaHtml('Contenu', 'modele_content', (isset($data['content']))?$data['content']:'', 'publication', (isset($data['id']))?$data['id']:0, array('class_input'=>'editor')); ?>
        <?php echo $this->getTextareaHtml('Résumé', 'modele_summary', (isset($data['summary']))?$data['summary']:'', 'publication', (isset($data['id']))?$data['id']:0, array('class_input'=>'editor')); ?>
        <?php include('inc/seoManager.inc.php'); ?>
        <?php include('inc/redirectionManager.inc.php'); ?>
        <div align="right">
            <input type="submit" value="Enregistrer" class="pwe-ui-btn"/> 
            <?php if(isset($data['id'])&&$data['id']>0): ?><a href="<?php echo $data['url']; ?>" target="_blank" title="voir la publication" class="pwe-ui-btn">voir</a><?php endif; ?>
        </div>
    </div>
    <div class="columns four">
        <?php include('inc/statutManager.inc.php'); ?>
        <br/><br/>
        <?php include('inc/datePublicationManager.inc.php'); ?>
        <br/><br/>
        <?php include('inc/templateManager.inc.php'); ?>
        <br/><br/>
        <h3>Rubrique</h3>
        <select name="modele_category_id">
            <option></option>
            <?php foreach(modele_category::getPrimaryListe() as $category): ?>
            <?php
                if(isset($data['category_id'])&&$data['category_id']==$category->getId() || (!isset($data['id']) && $_GET['category_id']==$category->getId())){
                    $selected = ' selected="selected"';
                }else{
                    $selected = '';
                }
            ?>
            <option <?php echo ($category->isPage())?'class="desable"':'value="'.$category->getId().'"'; ?><?php echo $selected; ?>><?php echo $category->getTitle(); ?></option>    
                <?php if($category->haveChild()): ?>
                <?php foreach($category->getChild() as $sCategory): ?>
                <?php
                    if((isset($data['category_id'])&&$data['category_id']==$sCategory->getId()) || (!isset($data['id']) && $_GET['category_id']==$sCategory->getId())){
                        $selected = ' selected="selected"';
                    }else{
                        $selected = '';
                    }
                ?>
            <option <?php echo (get_class($sCategory)=='modele_category' && $sCategory->isPage())?'class="desable"':'value="'.$sCategory->getId().'"'; ?><?php echo $selected; ?>> - <?php echo $sCategory->getTitle(); ?></option>
                
                    <?php if($sCategory->haveChild()): ?>
                    <?php foreach($sCategory->getChild() as $ssCategory): ?>
                    <?php
                        if((isset($data['category_id'])&&$data['category_id']==$ssCategory->getId()) || (!isset($data['id']) && $_GET['category_id']==$ssCategory->getId())){
                            $selected = ' selected="selected"';
                        }else{
                            $selected = '';
                        }
                    ?>
                    <option <?php echo ($ssCategory->isPage())?'class="desable"':'value="'.$ssCategory->getId().'"'; ?><?php echo $selected; ?>> - - <?php echo $ssCategory->getTitle(); ?></option>
                    <?php endforeach; ?>
                    <?php endif; ?>
                    
                <?php endforeach; ?>
                <?php endif; ?>            
            <?php endforeach;?>
        </select><br/><br/>
        <?php include('inc/mediaManager.inc.php'); ?>
        <br/><br/>
        <?php include('inc/tagManager.inc.php'); ?>
        <input type="hidden" name="modele_id" value="<?php echo (isset($data['id']))?$data['id']:0; ?>" />
    </div>
</form>
<?php else: ?>
<?php
// enregistrer le changement d'ordre
if(isset($_POST) && isset($_POST['save_order'])){
    foreach($_POST as $var=>$value){
        if(substr($var,0,6)==='order_'){
            list($s, $idp) = explode('_',$var,2);
            $publication = new modele_publication($idp);
            $publication->setOrder($value);
        }
    }
}

if(isset($_GET['category_id'])&&$_GET['category_id']>0){
    $category = new modele_category($_GET['category_id']);
    $liste = $category->getPublications();
    $title = 'Liste des articles de la rubrique "'.$category->getTitle().'"';
    $addonCreate = '&category_id='.$category->getId();
    $sortable = true;
}elseif(isset($_GET['tag_id'])&&$_GET['tag_id']>0){
    $category = new modele_tag($_GET['tag_id']);
    $liste = $category->getPublications();
    $title = 'Liste des articles rattachés au tag "'.$category->getTitle().'"';
    $addonCreate = '&tag_id='.$category->getId();
    $sortable = false;
}else{
    $liste = modele_publication::getListe();
    $title = 'Liste des articles';
    $addonCreate = '';
    $sortable = false;
}
?>
<div class="columns thirteen">
    <h1><?php echo $title; ?></h1>
    <hr style="margin: 5px 0 25px 0;">
    <div align="right">
        <a class="pwe-ui-btn" href="?template=publication&modele=publication&action=create<?php echo $addonCreate; ?>">Créer un article</a>
    </div>
    <br/>
    <?php if($sortable): ?><form method="post"><?php endif; ?>
    <table class="liste ui-widget<?php if($sortable): ?> table_sortable<?php endif; ?>" width="100%">
        <thead class="ui-widget-header">
            <tr>
                <th></th>
                <th>Titre</th>
                <th>Rubrique</th>
                <th>Template</th>
                <th>Dernière modification</th>
                <th>Auteur</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody<?php if($sortable): ?> class="liste-items-menu"<?php endif; ?>>
            <?php foreach($liste as $i=>$publication): ?>
            <?php if($publication->getCategory()->isPage()){
                $publication = new modele_page($publication->getId());
            }
            ?>
            <tr<?php if($sortable): ?> style="cursor: move;"<?php endif; ?>>
                <td>
                    <?php echo $i+1; ?>
                    <?php if($sortable): ?>
                    <input type="hidden" class="orderValue" name="order_<?php echo $publication->getId(); ?>" value="<?php echo $i+1; ?>" />
                    <?php endif; ?>
                </td>
                <td>
                    <span><?php echo $publication->getTitle(); ?></span>
                </td>
                <td><?php echo ($publication->getCategory()!==null)?str_replace(PWE::getBaseURL(), '', $publication->getCategory()->getPermalien()->getCleanedLien(false)):'-'; ?></td>
                <td><?php echo $publication->getTemplate(); ?></td>
                <td>
                    <?php echo $publication->getDate(); ?><br/>
                    <b><?php echo $publication->getStatut(); ?></b>
                </td>
                <td>
                    <b><?php echo $publication->getAuteur(); ?></b><br/>
                    (<?php echo $publication->getAuteurLevel(); ?>)
                </td>
                <td>
                    <a class="pwe-ui-btn" href="<?php echo $publication->getUrl(); ?>" target="preview_site" title="Voir"><span class="ui-icon ui-icon-extlink"></span></a>
                    <a class="pwe-ui-btn" href="?template=<?php echo $publication->type; ?>&modele=<?php echo $publication->type; ?>&action=update&modele_id=<?php echo $publication->getId().$addonCreate; ?>"><span class="ui-icon ui-icon-pencil"></span></a>
                    <a href="?template=<?php echo $publication->type; ?>&modele=<?php echo $publication->type; ?>&action=delete&do=1&modele_id=<?php echo $publication->getId().$addonCreate; ?>" class="confirmAction pwe-ui-btn" title="Supprimer l'article '<?php echo $publication->getTitle(); ?>'"><span class="ui-icon ui-icon-trash"></span></a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot></tfoot>
    </table>
    <?php if($sortable): ?>
    <br/><div align="right"><br/>(modifier l'ordre en déplaçant les lignes par glisser/déposer) <input class="pwe-ui-btn" type="submit" name="save_order" value="enregistrer l'ordre" /></div>
</form>
    <?php endif; ?>
</div>
<?php endif; ?>